angular.module("app").directive("appCountdown", ["$interval", "product", function($interval, product) {

	return {
		restrict: "AE",
		replace: true,
		transclude: true,
		templateUrl: "views/template/Countdown.html",
		scope: {
			time: "@"
		},
		link: function(scope, ele, attr) {
			var miao = null
			scope.time = scope.time.split(":")
			miao = (parseInt(scope.time[0] * 3600)) + (parseInt(scope.time[1] * 60)) + (parseInt(scope.time[2]))
			var timer = $interval(function() {
				if(miao >= 0) {
					scope.time = addZero(Math.floor(miao / 3600)) + ":" + addZero(Math.floor(miao / 60) % 60) + ":" + addZero(miao % 60)
				} else {
					$interval.cancel(timer)
					window.location.reload()
				}
				miao = miao - 1
			}, 1000)
			
			product.timers.push(timer)

			function addZero(num) {
				if(num < 10) {
					return "0" + num.toString()
				} else {
					return num
				}
			}

		}

	}

}])